package pe.com.modulo.cliente;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import pe.com.connection.bd.mysql.conexionMYSQL;



@SuppressWarnings("serial")
@WebServlet("/mantecliente")
public class manteClienteServlet extends HttpServlet {

	
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			
		System.out.println("POST");
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String metodo = request.getParameter("metodo");
		
		Gson gson = new Gson();
		String json = null;
		
		String sNombres = request.getParameter("strNombres");
		String sApellidos = request.getParameter("strApellidos");
		String sDNI = request.getParameter("strDNI");
		String sEmail = request.getParameter("strEmail");
		String sFecNac = request.getParameter("strFecNac");
		String sSexo = request.getParameter("strSexo");
		String sComentarios = request.getParameter("strComentarios");
		String sRef1 = request.getParameter("strRef1");
		String sTelRef1 = request.getParameter("strTelRef1");
		String sRef2 = request.getParameter("strRef2");
		String sTelRef2 = request.getParameter("strTelRef2");
		String sDireccion = request.getParameter("strDireccion");
		String sTelefono = request.getParameter("strTelefono");
		String sCelular = request.getParameter("strCelular");
		
		//Datos del Vehiculo
		String sPlaca = request.getParameter("strPlaca");
		String sModelo = request.getParameter("strModelo");
		String sMarca = request.getParameter("strMarca");
		String sAnio = request.getParameter("strAnio");
		String sColor = request.getParameter("strColor");
		String sComentarios2 = request.getParameter("strComentarios2");

		if (metodo.equals("agregar")) {

			boolean agregado = false;
			try {

				conexionMYSQL c = new conexionMYSQL();
				Connection con = c.getConexion();
				if (con != null) {
					Statement st;
					st = con.createStatement();
					st.executeUpdate("INSERT INTO cliente"
							+ "(`nombres`,`apellidos`,`dni`, `email`, `fec_nacimiento`,`sexo`,`comentarios`,`ref1`,`tel_ref1`,`ref2`,`tel_ref2`,`direccion`,`telefono`,`celular` ) "
							+ "VALUES ('"+ sNombres+ "','"+ sApellidos+ "','"+ sDNI+ "','"+ sEmail+ "','"+ sFecNac+ "','"+ sSexo+ "','"
							+ sComentarios+ "','"+ sRef1+ "','"	+ sTelRef1+ "','"+ sRef2+ "','"	+ sTelRef2+ "','"+ sDireccion+ "','"+ sTelefono	+ "','"	+ sCelular+ "')");
					
					// Ingresar Vehiculo
					st.executeUpdate("INSERT INTO vehiculo"
							+ "(`placa`,`modelo`,`marca`, `anio`, `color`,`comentarios` ) "
							+ "VALUES ('"+ sPlaca+ "','"+ sModelo+ "','"+ sMarca+ "','"+ sAnio+ "','"+ sColor+ "','"+ sComentarios2+ "')");
					agregado = true;
					st.close();
					json = gson.toJson("Cliente Registrado Satisfactoriamente");
				}
				c.cerrarConexion();
				

				

			} catch (SQLException e) {
				agregado = false;
				//e.printStackTrace();
				json = gson.toJson(e.getMessage());
				System.out.println("ERROR : "+e.getMessage());
			}
			
			request.setCharacterEncoding("utf-8");
			response.setContentType("application/json;charset=utf-8");
			PrintWriter out = response.getWriter();		
			out.print(json);
			out.flush();

		}

		if (metodo.equals("eliminar")) {

		}

	}

}
